const util = require('../../../utils/util.js')
const ajax = require('../../../utils/ajax/ajax.js')
Page({
    data: {
        region: '',
        fileList: [],
        textCount: 0,
        isDisabled: true,
        formData: {
            regionId: '',
            address: '',
            des: ''
        },
        isArea: true
        
    },
    onLoad: function (options) {

    },
    deleteImg(e) {
        const delIndex = e.detail.index
        const fileList = this.data.fileList
        fileList.splice(delIndex, 1)
        this.setData({
            fileList
        })
    },
    afterRead(e) {
        const that = this
        const url = e.detail.file.url
        util.showLoading('图片上传中')
        util.uploadImg({
            url,
            success(res) {
                const fileList = that.data.fileList
                fileList.push({
                    url: res.data
                })
                that.setData({
                    fileList
                })
                util.hideLoading()
            },
            fail() {
                util.toast('图片上传失败')
            },
            complete() {
                util.hideLoading()
            }
        })
    },
    displayArea() {
        const area = this.selectComponent('#area')
        area.showArea();
    },
    areaConfirm(e) {
        const value = e.detail.detail.values
        const formData = this.data.formData
        formData.regionId = value[2].code
        this.setData({
            region: `${value[0].name} ${value[1].name} ${value[2].name}`,
            formData: formData
        })
        this.displayArea()
    },
    onAreaClose(e) {
        const isArea = e.detail.isArea
        this.setData({
            isArea: isArea
        })
        console.log(isArea);
    },
    inputChange(e) {
        const dataset = e.currentTarget.dataset
        const formData = this.data.formData
        const value = e.detail.value
        formData[dataset.label] = value
        
        if (dataset.label === 'des') {
            const cursor = e.detail.cursor
            this.setData({
                textCount: cursor
            })
        }

        this.setData({
            formData: formData,
            isDisabled: false
        })
    },
    toCommitSuccess() {
        console.log(this.data.formData);
        const fileList = this.data.fileList
        const { regionId, address, des } = this.data.formData

        if (!regionId) {
            util.toast('请选择所在地区')
            return
        }

        if (!address) {
            util.toast('请填写街道')
            return
        }

        if (fileList.length === 0) {
            util.toast('请上传图片')
            return
        }

        if (!des) {
            util.toast('请填写补充说明')
            return
        }

        let imgs = '';
        for (let i = 0; i < fileList.length; i++) {
            const json = fileList[i].url;
            imgs += json + ',';
        }
        const pic = imgs.substring(0, imgs.lastIndexOf(','))
        console.log(pic);
        ajax.post({
            url: '/wx/submitSpaceRec',
            data: {
                region_id: regionId,
                street: address,
                pic: pic,
                remark: des
            },
            success() {
                wx.navigateTo({
                    url: '/pages/Index/commitSuccess/commitSuccess',
                })
            }
        })
    }
})